package com.pan.controller;


import com.pan.entity.Result;
import com.pan.entity.Stock;
import com.pan.service.StockService;
import com.pan.vo.StockInsertVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/stock")
public class StockController {


    @Resource
    private StockService stockService;

    @ResponseBody
    @GetMapping("/select/id/{id}")
    public Result<Stock> selectById(@CookieValue("adminTicket") String adminTicket,
                             @PathVariable Long id){
        Result<Stock> result = stockService.selectById(adminTicket, id);
        return result;
    }

    @ResponseBody
    @PostMapping("/insert")
    public Result insert(@CookieValue("adminTicket") String adminTicket,
                         @RequestBody StockInsertVo stockInsertVo){
        Result result = stockService.insert(adminTicket, stockInsertVo);
        return result;
    }

    @ResponseBody
    @GetMapping("/getGoodsCount/{goodsId}")
    public Result<Long> getGoodsCount(@PathVariable Long goodsId){
        Result<Long> result = stockService.getGoodsCount(goodsId);
        return result;
    }

    @ResponseBody
    @GetMapping("/select/goodsId/{goodsId}")
    public Result<List<Stock>> selectByGoodsId(@CookieValue("adminTicket") String adminTicket,
                                               @PathVariable Long goodsId){
        Result<List<Stock>> result  = stockService.selectByGoodsId(adminTicket,goodsId);
        return result;
    }

    @ResponseBody
    @GetMapping("/select/goodsId/all/{goodsId}")
    @ApiOperation("包含已经清仓的的库存")
    public Result<List<Stock>> selectByAllGoodsId(@CookieValue("adminTicket")String adminTicket,
                                     @PathVariable Long goodsId){
        Result<List<Stock>> result = stockService.selectAllByGoodsId(adminTicket,goodsId);
        return result;
    }

    @ResponseBody
    @PostMapping("/reduce/{id}/{reduceCount}")
    public Result reduceGoodsCount(@CookieValue("adminTicket") String adminTicket,
                                   @PathVariable("id") Long id,
                                   @PathVariable("reduceCount") Long reduceCount){
        Result result = stockService.updateReduceStock(adminTicket,id,reduceCount);
        return result;
    }


    @ResponseBody
    @PostMapping("/reduce/noCookie/{id}/{reduceCount}")
    public Result reduceGoodsCountNoCookie( String adminTicket,
                                   @PathVariable("id") Long id,
                                   @PathVariable("reduceCount") Long reduceCount){
        Result result = stockService.updateReduceStock(adminTicket,id,reduceCount);
        return result;
    }





}
